﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml.XPath;
using System.Xml;
using System.Data;
using umbraco.DataLayer;
using System.IO;

namespace UComment
{
    public class XSLTLibrary
    {
        public static string getGravatar(string email, int size, string defaultImage)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(email));

            System.Text.StringBuilder hash = new System.Text.StringBuilder();
            for (int i = 0; i < result.Length; i++)
                hash.Append(result[i].ToString("x2"));

            System.Text.StringBuilder image = new System.Text.StringBuilder();

            image.Append("http://www.gravatar.com/avatar.php?");
            image.Append("gravatar_id=" + hash.ToString());
            image.Append("&amp;rating=G");
            image.Append("&amp;size=" + size.ToString());
            image.Append("&amp;default=");
            image.Append(System.Web.HttpContext.Current.Server.UrlEncode(defaultImage));


            return image.ToString();
        }


        public static string Dictionary(string text, string fallback)
        {
            string key = text.Trim().TrimStart('#');
            string result = umbraco.library.GetDictionaryItem(key);

            if (result.Length > 0)
                return result;
            else if (!string.IsNullOrEmpty(fallback))
                return fallback;

            return key;
        }

        public static XPathNodeIterator GetComments()
        {           
            ISqlHelper SqlHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);

            IRecordsReader rr = SqlHelper.ExecuteReader(
                "select * from comment where spam != 1");

            return CommentsToXml(rr);
        }

        //public static XPathNodeIterator GetCommentsForBlog(int nodeid)
        //{
        //    ISqlHelper SqlHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);

        //    IRecordsReader rr = SqlHelper.ExecuteReader(
        //        "select * from comment where mainid = @mainid and spam != 1",
        //        SqlHelper.CreateParameter("@mainid", nodeid));

        //    return CommentsToXml(rr);
        //}

        public static XPathNodeIterator GetCommentsForNode(int nodeid)
        {

            ISqlHelper SqlHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);
           
            IRecordsReader rr = SqlHelper.ExecuteReader(
                "select * from comment where nodeid = @nodeid and spam != 1",
                 SqlHelper.CreateParameter("@nodeid", nodeid));

            return CommentsToXml(rr);
        }

        private static XPathNodeIterator CommentsToXml(IRecordsReader rr)
        {
            XmlDocument xd = new XmlDocument();

            XmlNode x = umbraco.xmlHelper.addTextNode(xd, "comments", "");

            while (rr.Read())
            {
                XmlNode c = xd.CreateElement("comment");

                c.Attributes.Append(umbraco.xmlHelper.addAttribute(xd, "id", rr.GetInt("id").ToString()));
                c.Attributes.Append(umbraco.xmlHelper.addAttribute(xd, "nodeid", rr.GetInt("nodeid").ToString()));
                c.Attributes.Append(umbraco.xmlHelper.addAttribute(xd, "created", rr.GetDateTime("created").ToString()));

                c.AppendChild(umbraco.xmlHelper.addCDataNode(xd, "name", rr.GetString("name")));
                c.AppendChild(umbraco.xmlHelper.addCDataNode(xd, "email", rr.GetString("email")));
                c.AppendChild(umbraco.xmlHelper.addCDataNode(xd, "website", rr.GetString("website")));
                c.AppendChild(umbraco.xmlHelper.addCDataNode(xd, "message", rr.GetString("comment")));

                x.AppendChild(c);
            }

            xd.AppendChild(x);

            return xd.CreateNavigator().Select(".");
        }

      
    }
}
